package com.anhry.app.safety.manage.tjfx.struts.base;


import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.service.SystemService;
import com.anhry.app.safety.manage.tjfx.bean.vo.TjfxCallManageBean;
import com.anhry.app.safety.manage.tjfx.service.TjfxService;
import com.anhry.app.safety.util.DateFormater;
import com.anhry.app.safety.util.Page;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.action.UtilBaseAction;

@Controller
@SuppressWarnings("unchecked")
@Scope("prototype")
public class TjfxOaBaseAction extends UtilBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	protected TjfxService tjfxService;

	@Resource
	protected SystemService systemService;

	protected Page page;

	protected String num;

	protected String size;

	protected String result;// 返回页面
	
	private String tip;
	
	protected String keyWord;

	protected String keyTemp;

	protected String search;

	private String beginTime;

	private String endTime;

	private String cGrade;

	private String cUnit;

	private String cDept;

	private String cRole;

	private String gradeId;

	private String unitId;

	private String key;

	private String xmlString;

	private String tbXmlString;

	private String hbXmlString;

	private String showId;
	
	private String tjType;
	
	private String quId;


	/**
	 * 初始页码
	 * 
	 * @param num
	 *            页数
	 * @return 页数
	 */
	public int setPageNum(int num) {
		return (this.num != null && !"".equals(this.num)) ? Integer.parseInt(this.num) : num;
	}

	/**
	 * 定义分页的每页显示的初始个数
	 * 
	 * @param size
	 *            显示数
	 * @return 显示数
	 */
	public int setPageSize(int size) {
		return (this.size != null && !"".equals(this.size)) ? Integer.parseInt(this.size) : size;
	}
	
	/**
	 * 初始化图表所用的xml字符串
	 * 
	 * @param beanList
	 */
	public void initXmlString(List<TjfxCallManageBean> beanList, String title, String x, String y) {
//		StringBuffer tempBuffer = new StringBuffer("<?xml version='1.0' encoding='utf-8'?><chart formatNumberScale='0' showPercentInToolTip='0' showPercentValues='1' forceDecimals='0' decimals='2' baseFontSize='14' useRoundEdges='1' bgColor='FFFFFF,FFFFFF' showBorder='0' exportEnabled='1' exportFormats='JPEG=导出为JPG图片|PNG=导出为PNG图片|PDF=导出为PDF文件' exportAtClient='0' exportAction='download'  caption='" + title + "' xAxisName='" + x + "' yAxisName='" + y + "' exportFileName='pdf_image' exportHandler='http://localhost:8080/shySafety/FCExporterDirectPic.jsp'  >");
		StringBuffer tempBuffer = new StringBuffer();
		StringBuffer tempBuffer2 = new StringBuffer("<?xml version='1.0' encoding='utf-8'?><chart caption='" + title + "' xAxisName='" + x + "' yAxisName='" + y + "' formatNumberScale='0' formatNumber='0'  decimalPrecision='2'  baseFontSize='14' useRoundEdges='1' bgColor='FFFFFF,FFFFFF' showBorder='0' exportEnabled='1' exportFormats='JPEG=导出为JPG图片|PNG=导出为PNG图片|PDF=导出为PDF文件'  exportAtClient='0' exportAction='download' exportHandler='http://localhost:8080/shySafety/FCExporterDirectPic.jsp' exportFileName='image_pdf'  >");

		StringBuffer xString = new StringBuffer("<categories font='" + title + "' fontSize='15' fontColor='000000'>");
		StringBuffer nowString = new StringBuffer("<dataset seriesname='本期次数' color='C9198D' alpha='100'>");
		StringBuffer tbString = new StringBuffer("<dataset seriesname='同比次数' color='FDC12E' alpha='100'>");
		StringBuffer hbString = new StringBuffer("<dataset seriesname='环比次数' color='FDC12E' alpha='100'>");
		if (beanList.size() > 0) {
			for (TjfxCallManageBean bean : beanList) {
				if (!bean.getShowName().equals("合计")) {
					tempBuffer.append("<set name='" + bean.getShowName() + "' value='" + bean.getPrivateCount() + "' />");
					xString.append("<category name='" + bean.getShowName() + "'/>");
					nowString.append("<set value='" + bean.getPrivateCount() + "'/>");
					tbString.append("<set value='" + bean.getTbCount() + "'/>");
					hbString.append("<set value='" + bean.getHbCount() + "'/>");
				}				
			}
			// 同比环比图形
			xString.append("</categories>");
			nowString.append("</dataset>");
			tbString.append("</dataset>");
			hbString.append("</dataset>");
//			tempBuffer.append("</chart>");
		} else {
			tempBuffer.append("<set name='' value=''></chart>");
			tempBuffer2.append("");
		}
		String s = tempBuffer2.append(xString).append(nowString).toString();
		tbXmlString = s+tbString.toString()+"</chart>".toString();
		hbXmlString = s+hbString.toString()+"</chart>".toString();
		xmlString = tempBuffer.toString();
	}

	public String initCountParameter(StringBuffer countHql, String cellData){
		TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		int grade = Integer.parseInt(user.getTbUserGrade().getId()+"");
		if (null != this.cGrade && this.cGrade.length() > 0) {
			countHql.append(" and g.id=" + this.cGrade);
		}
		if (null != this.showId && this.showId.length() > 0) {
			countHql.append(" and " + showId.split(":")[0]+"="+showId.split(":")[1]);
		}
		//权限条件
		if (grade != 0 && grade != 1 && grade != 2){
			if (grade == 7){ //属地
				if (user.getTbUserDept() != null){//乡镇级别
					countHql.append(" and d.id = ").append(user.getTbUserDept().getId());
				} else {//区属地 能查询区行业和区科室的数据
					countHql.append(" and (d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(") or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 2) or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 1)");
				}
			}else if (grade == 5){//市行业
				if (user.getTbUserDept() != null){
					countHql.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" or d.dept_sj=").append(user.getTbUserDept().getId());
				} else {
					countHql.append(" and d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_sj=").append(user.getTbUserUnit().getId());
				}
			} else if (grade == 6){//区行业
				if (user.getTbUserDept() != null){
					countHql.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" and t.deptType=2");
				} else {
					countHql.append(" and d2.id = ").append(user.getTbUserUnit().getId());
				}
			} else if (grade == 3){//市科室 查询市科室和区科室数据
				countHql.append(" and d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_sj=").append(user.getTbUserUnit().getId());
			} else if (grade == 4){//区科室 查询区科室数据
				countHql.append(" and d.id = ").append(user.getTbUserDept().getId());
			} else if (grade == 8){//区安办 区科室 区行业 属地 区安办
				countHql.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 1) or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 2)").append(" or (d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(")");
			}
		}
		
//		if (tjType == null || tjType.equals("")){
//			tjType = user.getTbUserGrade().getId().toString();
//		}
//			if (tjType.equals("7")){ //属地 判断菜单点击
//				//判断用户
//				if (grade == 7 && user.getTbUserDept() != null){//乡镇级别
//					countHql.append(" and d.id = ").append(user.getTbUserDept().getId());
//				} else  {//区属地
//					if (!quId.equals("") && quId != null){//点击属地名 查看属地用户和所属乡镇的分别统计
//						
//					} else {//查看属地用户和乡镇总的统计数目
//						countHql.append(" and (d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_parent_id = ").append(user.getTbUserUnit().getId());
//					}
//				}
//			}else if (tjType.equals("5")){//市行业
//				if (user.getTbUserDept() != null){
//					countHql.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" or d.dept_sj=").append(user.getTbUserDept().getId());
//				} else {
//					countHql.append(" and d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_sj=").append(user.getTbUserUnit().getId());
//				}
//			} else if (tjType.equals("6")){//区行业
//				if (user.getTbUserDept() != null){
//					countHql.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" and t.deptType=2");
//				} else {
//					countHql.append(" and d2.id = ").append(user.getTbUserUnit().getId());
//				}
//			} else if (tjType.equals("3")){//市科室 查询市科室和区科室数据
//				countHql.append(" and d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_sj=").append(user.getTbUserUnit().getId());
//			} else if (tjType.equals("4")){//区科室 查询区科室数据
//				countHql.append(" and d.id = ").append(user.getTbUserDept().getId());
//			} else if (tjType.equals("8")){//区安办 区科室 区行业 属地 区安办
//				countHql.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 1) or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 2)").append(" or (d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(")");
//			}
		
		Calendar ca = Calendar.getInstance();
		String beginTime = null;
		String endTime = null;
		if (this.beginTime == null || "".equals(this.beginTime) || this.endTime == null || "".equals(this.endTime)) {
			beginTime = ca.get(Calendar.YEAR) + "-1-1";
			endTime = DateFormater.formatDate(ca);
		} else {
			beginTime = this.beginTime;
			endTime = this.endTime;
		}
		countHql.append(" and "+cellData+" between to_date('" + beginTime + " 00:00:00','yyyy-MM-dd HH24:mi:ss')").append(" and to_date('" + endTime + " 23:59:59','yyyy-MM-dd HH24:mi:ss')");
		return countHql.toString();
	}
	public String getNum() {
		return num;
	}
	
	/**
	 * 关键字解码
	 */
	public void checkSearch() {
		if (this.getSearch() == null || !"1".equals(this.getSearch())) {
			key = this.contextPvd.decode(key);
		}
	}
	
	public void initTime(StringBuffer nowSql, String cellDate) throws ParseException {
		Calendar ca = Calendar.getInstance();
		String beginTime = null;
		String endTime = null;
		if (this.beginTime == null || "".equals(this.beginTime) || this.endTime == null || "".equals(this.endTime)) {
			beginTime = ca.get(Calendar.YEAR) + "-1-1";
			endTime = DateFormater.formatDate(ca);
		} else {
			beginTime = this.beginTime;
			endTime = this.endTime;
		}
		Date d1 = DateFormater.formatDate(beginTime);
		Date d2 = DateFormater.formatDate(endTime);
		String d3 = DateFormater.formatDate(new Date(d1.getTime() - (d2.getTime() - d1.getTime()))); // 环比开始时间
		ca.setTime(d1);
		ca.set(Calendar.YEAR, ca.get(Calendar.YEAR) - 1);
		String d4 = DateFormater.formatDate(ca);// 同比开始时间
		ca.setTime(d2);
		ca.set(Calendar.YEAR, ca.get(Calendar.YEAR) - 1);
		String d5 = DateFormater.formatDate(ca);// 同比结束时间
		nowSql.append(" sum(case when ").append(cellDate).append(" between to_date('" + beginTime + " 00:00:00','yyyy-MM-dd HH24:mi:ss')").append(" and to_date('" + endTime + " 23:59:59','yyyy-MM-dd HH24:mi:ss') then 1 else 0 end) nowCount,").append(" sum(case when ").append(cellDate).append(" between to_date('" + d4 + " 00:00:00','yyyy-MM-dd HH24:mi:ss')").append(" and to_date('" + d5 + " 23:59:59','yyyy-MM-dd HH24:mi:ss') then 1 else 0 end) tbCount,").append(" sum(case when ").append(cellDate).append(" between to_date('" + d3 + " 00:00:00','yyyy-MM-dd HH24:mi:ss')").append(" and to_date('" + beginTime + " 00:00:00','yyyy-MM-dd HH24:mi:ss') then 1 else 0 end) hbCount,");
		this.tip = "本次搜索内容中，同比为 " + beginTime + "至" + endTime + "比" + d4 + "至" + d5 + "，环比为 " + beginTime + "至" + endTime + "比" + d3 + "至" + beginTime;
	}
	
	public String initParameter(String groupString, String orderString) {
		TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		int grade = Integer.parseInt(user.getTbUserGrade().getId()+"");
//		int grade = Integer.parseInt(s)getCGrade();
		StringBuffer parameterBuffer = new StringBuffer();
		if (null != this.cGrade && this.cGrade.length() > 0) {
			parameterBuffer.append(" and g.id=" + this.cGrade);
		}
		if (null != this.showId && this.showId.length() > 0) {
			parameterBuffer.append(" and " + showId.split(":")[0]+"="+showId.split(":")[1]);
		}
		/*
		 * if （属地统计）
		 */
		//权限条件
		if (grade != 0 && grade != 1 && grade != 2){
			if (grade == 7){ //属地
				if (user.getTbUserDept() != null){//乡镇级别
					parameterBuffer.append(" and d.id = ").append(user.getTbUserDept().getId());
				} else {//区属地 能查询区行业和区科室的数据
					parameterBuffer.append(" and (d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(") or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 2) or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 1)");
				}
			}else if (grade == 5){//市行业
				if (user.getTbUserDept() != null){
					parameterBuffer.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" or d.dept_sj=").append(user.getTbUserDept().getId());
				} else {
					parameterBuffer.append(" and d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_sj=").append(user.getTbUserUnit().getId());
				}
			} else if (grade == 6){//区行业
				if (user.getTbUserDept() != null){
					parameterBuffer.append(" and d.id = ").append(user.getTbUserDept().getId());
				} else {
					parameterBuffer.append(" and d2.id = ").append(user.getTbUserUnit().getId());
				}
			} else if (grade == 3){//市科室 查询市科室和区科室数据
				parameterBuffer.append(" and d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_sj=").append(user.getTbUserUnit().getId());
			} else if (grade == 4){//区科室 查询区科室数据
				parameterBuffer.append(" and d.id = ").append(user.getTbUserDept().getId());
			} else if (grade == 8){//区安办 区科室 区行业 属地 区安办
				parameterBuffer.append(" and d.id = ").append(user.getTbUserDept().getId()).append(" or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 1) or (d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(" and d.dept_grade = 3 and d.dept_type = 2)").append(" or (d2.id = ").append(user.getTbUserUnit().getId()).append(" or d.dept_parent_id = ").append(user.getTbUserUnit().getId()).append(")");
			}
		}
		
		// 分组字符串 排序字符串
		if (null != groupString && groupString.length() > 0) {
			parameterBuffer.append(" group by ").append(groupString);
		}
//		if (null != orderString && orderString.length() > 0) {
//			parameterBuffer.append(" order by ").append(orderString);
//		}
		return parameterBuffer.toString();
	}
	

	public void setNum(String num) {
		this.num = num;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public TjfxService getTjfxService() {
		return tjfxService;
	}

	public void setTjfxService(TjfxService tjfxService) {
		this.tjfxService = tjfxService;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getCDept() {
		return cDept;
	}

	public void setCDept(String dept) {
		cDept = dept;
	}

	public String getCGrade() {
		return cGrade;
	}

	public void setCGrade(String grade) {
		cGrade = grade;
	}

	public String getCRole() {
		return cRole;
	}

	public void setCRole(String role) {
		cRole = role;
	}

	public String getCUnit() {
		return cUnit;
	}

	public void setCUnit(String unit) {
		cUnit = unit;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getGradeId() {
		return gradeId;
	}

	public void setGradeId(String gradeId) {
		this.gradeId = gradeId;
	}

	public String getHbXmlString() {
		return hbXmlString;
	}

	public void setHbXmlString(String hbXmlString) {
		this.hbXmlString = hbXmlString;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getKeyTemp() {
		return keyTemp;
	}

	public void setKeyTemp(String keyTemp) {
		this.keyTemp = keyTemp;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getQuId() {
		return quId;
	}

	public void setQuId(String quId) {
		this.quId = quId;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getShowId() {
		return showId;
	}

	public void setShowId(String showId) {
		this.showId = showId;
	}

	public String getTbXmlString() {
		return tbXmlString;
	}

	public void setTbXmlString(String tbXmlString) {
		this.tbXmlString = tbXmlString;
	}

	public String getTjType() {
		return tjType;
	}

	public void setTjType(String tjType) {
		this.tjType = tjType;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getXmlString() {
		return xmlString;
	}

	public void setXmlString(String xmlString) {
		this.xmlString = xmlString;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

}
